/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module1;


import dao.QuiDinhDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.QuiDinh;

/**
 *
 * @author OSIRIS
 */
public class QuiDinhFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form QuiDinhFrame
     */
    public QuiDinhFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jBtnThem = new javax.swing.JButton();
        jBtnXoa = new javax.swing.JButton();
        jBtnSua = new javax.swing.JButton();

        setClosable(true);
        setTitle("Danh Sách Qui Định");
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jBtnThem.setText("Thêm ");
        jBtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnThemActionPerformed(evt);
            }
        });

        jBtnXoa.setText("Xóa");
        jBtnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnXoaActionPerformed(evt);
            }
        });

        jBtnSua.setText("Sửa");
        jBtnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSuaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBtnThem)
                    .addComponent(jBtnXoa)
                    .addComponent(jBtnSua))
                .addGap(37, 37, 37))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnThem)
                        .addGap(28, 28, 28)
                        .addComponent(jBtnXoa)
                        .addGap(31, 31, 31)
                        .addComponent(jBtnSua)))
                .addContainerGap(84, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void load(){
         QuiDinhDAO dmDao = new QuiDinhDAO();
        List ds = dmDao.findAll();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Qui Định");
        dtm.addColumn("Value");
        for (int i = 0; i < ds.size(); i++) {
            QuiDinh dm = (QuiDinh) ds.get(i);
            Vector vt = new Vector();
            vt.add(dm.getQd());
            vt.add(dm.getValue());
            dtm.addRow(vt);
        }
        this.jTable1.setModel(dtm);
    }
    private String getQdIDToTable() {
        int selectedRowIndex = jTable1.getSelectedRow();
        //int selectedColumnIndex = jTable.getSelectedColumn();
        String selectedObject = (String) jTable1.getModel().getValueAt(selectedRowIndex, 0);
        return selectedObject;
    }

    private int getQdValueToTable() {
        int selectedRowIndex = jTable1.getSelectedRow();
        //int selectedColumnIndex = jTable.getSelectedColumn();
        int selectedObject = (Integer) jTable1.getValueAt(selectedRowIndex, 1);
        return selectedObject;
    }
    private void jBtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnThemActionPerformed
        // TODO add your handling code here:
         try {
            QuiDinhThemDialog dlg = new QuiDinhThemDialog(null, true);
            dlg.setTitle("Thêm");
            dlg.setVisible(true);
            QuiDinhDAO dmDAO = new QuiDinhDAO();
            QuiDinh info = new QuiDinh();
            info.setQd(dlg.getQuidinh());
            info.setValue(dlg.getValue());
            dmDAO.create(info);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnThemActionPerformed

    private void jBtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnXoaActionPerformed
        // TODO add your handling code here:
        try {
            QuiDinhDAO dmDAO = new QuiDinhDAO();
            QuiDinh info = new QuiDinh();
            info.setQd(this.getQdIDToTable());
            dmDAO.delete(info);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnXoaActionPerformed

    private void jBtnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSuaActionPerformed
        // TODO add your handling code here:
        try {
            QuiDinhThemDialog dlg = new QuiDinhThemDialog(null, true);
            dlg.setTitle("Cập Nhật");
          
            dlg.setQuidinh(this.getQdIDToTable());
            dlg.setValue(this.getQdValueToTable());
            dlg.setVisible(true);
            QuiDinhDAO dmDAO = new QuiDinhDAO();
            QuiDinh info = new QuiDinh();
            info.setQd(dlg.getQuidinh());
            info.setValue(dlg.getValue());
            dmDAO.update(info);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnSuaActionPerformed

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        // TODO add your handling code here:
        
         try {
            load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_formAncestorAdded

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnSua;
    private javax.swing.JButton jBtnThem;
    private javax.swing.JButton jBtnXoa;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
